package com.tyros.common.utils;

import java.util.regex.Pattern;

/**
 * 正则工具类
 *
 * @author zhangshuaiyin
 * @date 2021-06-22 17:05
 */
public class RegexUtil {

    /**
     * 手机号、邮箱
     */
    public static final String PHONE_REGEX = "(\\+\\d+)?1\\d{10}$";
    public static final String EMAIL_REGEX = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
    /**
     * 身份证
     */
    public static final String ID_CARD_REGEX = "[1-9]\\d{13,16}[a-zA-Z0-9]";
    public static final String BIRTHDAY_REGEX = "\\d{4}(-)\\d{1,2}\\1\\d{1,2}";
    /**
     * 数字
     */
    public static final String INT_REGEX = "\\d+?";
    public static final String DECIMAL_REGEX = "-?[1-9]\\d+(\\.\\d+)?";
    /**
     * IP Port
     */
    private static final String IP_V4_REGEX = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$";
    private static final String PORT_REGEX = "^(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[0-5]\\d{4}|[1-9]\\d{0,3})$";
    private static final String DOMAIN_REGEX = "^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";

    /**
     * 验证邮箱
     *
     * @param email 邮箱
     * @return 校验是否成功
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(EMAIL_REGEX, email);
    }

    /**
     * 验证身份证
     *
     * @param idCard 身份证
     * @return 校验是否成功
     */
    public static boolean isIdCard(String idCard) {
        return Pattern.matches(ID_CARD_REGEX, idCard);
    }

    /**
     * 验证手机号
     *
     * @param phone 手机号
     * @return 校验是否成功
     */
    public static boolean isMobile(String phone) {
        return Pattern.matches(PHONE_REGEX, phone);
    }

    /**
     * 验证是否是整数
     *
     * @param num 整数
     * @return 校验是否成功
     */
    public static boolean isInteger(String num) {
        return Pattern.matches(INT_REGEX, num);
    }

    /**
     * 验证浮点数
     *
     * @param decimals 浮点数
     * @return 校验是否成功
     */
    public static boolean isDecimals(String decimals) {
        return Pattern.matches(DECIMAL_REGEX, decimals);
    }

    /**
     * 验证生日
     *
     * @param birthday 生日
     * @return 校验是否成功
     */
    public static boolean isBirthday(String birthday) {
        return Pattern.matches(BIRTHDAY_REGEX, birthday);
    }

    /**
     * 验证 IP
     *
     * @param ip IP
     * @return 校验是否成功
     */
    public static boolean isIp(String ip) {
        return Pattern.matches(IP_V4_REGEX, ip);
    }

    /**
     * 验证端口
     *
     * @param port 端口号
     * @return 校验是否成功
     */
    public static boolean isPort(String port) {
        return Pattern.matches(PORT_REGEX, port);
    }

    /**
     * 验证域名
     *
     * @param domain 域名
     * @return 校验是否成功
     */
    public static boolean isDomain(String domain) {
        return Pattern.matches(DOMAIN_REGEX, domain);
    }

    /**
     * IP 或 Port 大小比较
     *
     * @param start 起始IP或起始Port
     * @param end   截止IP或截止Port
     * @return 如果start小于end返回true，反之返回false
     */
    public static boolean compare(String start, String end) {
        boolean flag = false;
        String[] startIps = start.split("\\.");
        String[] endIps = end.split("\\.");
        for (int i = 0; i < startIps.length; i++) {
            if (Integer.parseInt(endIps[i]) > Integer.parseInt(startIps[i])) {
                flag = true;
                break;
            } else if (Integer.parseInt(endIps[i]) != Integer.parseInt(startIps[i])) {
                break;
            }
        }
        return flag;
    }

}
